package com.example.qiandao.controller;

import com.example.qiandao.common.Result;
import com.example.qiandao.controller.responsedto.CourseDTO;
import com.example.qiandao.entity.Course;
import com.example.qiandao.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;



    @GetMapping("/courselist/{teacherId}")
    public Result<List<CourseDTO>> getCourseList(@PathVariable Long teacherId) {
        List<CourseDTO> courseList = courseService.getCoursesByTeacherId(teacherId);
        return Result.success(courseList);
    }

    @PostMapping("/addcourse")
    public Result<String> addCourse(@RequestBody Course course) {
        boolean result = courseService.addCourse(course);
        if (result)
            return Result.success("添加课程成功");
        return Result.error(400, "添加课程失败");
    }
    @GetMapping("/{id}")
    public Result<Course> getCourseById(@PathVariable Long id) {
        Course course = courseService.getCourseById(id);
        if (course != null) {
            return Result.success(course);
        } else {
            return Result.error(404, "课程不存在");
        }
    }

    @PutMapping("/update")
    public Result<String> updateCourse(@RequestBody Course course) {
        boolean updated = courseService.updateCourse(course);
        if (updated) {
            return Result.success("课程更新成功");
        } else {
            return Result.error(400, "课程更新失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    public Result<String> deleteCourse(@PathVariable Long id) {
        boolean deleted = courseService.deleteCourse(id);
        if (deleted) {
            return Result.success("课程删除成功");
        } else {
            return Result.error(400, "课程删除失败");
        }
    }

    //--------------学生部分------------------------------------------------------------------------------------
    /**
     *  获取学生加入的课程
     *  @param studentId 学生id
     *  @return 返回值：课程列表
     * */
    @GetMapping("/student/courselist/{studentId}")
    public Result<List<CourseDTO>> getCoursesByStudentId(@PathVariable Long studentId) {
        List<CourseDTO> courses = courseService.getCoursesByStudentId(studentId);
        return Result.success(courses);
    }


}